/*-------------------------1.第一种方案-------------------------------------------------------*/
/*var deepClone1=function (obj) {
    return JSON.parse(JSON.stringify(obj))
}
var obj1 = {family:{brother:"wangzhipeng",father:"wanglicai",mother:"sunaiyun"},name:"gino",sex:"male",age:"27"};
var obj2 = deepClone1(obj1);
obj1.family.brother = "close";
console.log(obj1);
console.log(obj2);
 */
/*-------------------------1.第二种方案-------------------------------------------------------*/
var deepClone = function fnDeepClone(obj){
    var result = typeof obj.splice === 'function'?[]:{};
    if (obj && typeof obj === 'object'){
        for (key in obj ){
            if (obj[key] && typeof obj[key] === 'object'){
                result[key] = fnDeepClone(obj[key]);//如果对象的属性值为object的时候，递归调用deepClone，即再把某个值对象复制一份到新的对象的对应值中
            }else{
                result[key] = obj[key];//如果对象的属性值不为object的时候，直接复制参数对象的每一个键/值到新对象对应的键/值中
            }

        }
        return result;
    }
    return obj;


}

var deeClone = function fnDeepClone(obj) {
   var result= typeof obj.slice === 'function'?[]:{};
   if(obj && boj ==='object'){
       for(key in obj){
           if(obj[key]&& typeof obj[key] ==='object'){
               result[key]=fnDeepClone(obj[key])
           }else{
               result[key]=obj[key];
           }

       }
   }

}
//test array
var arry1= ["a","b","c","d"];
var arry2 = deepClone(arry1);
arry1.pop();
console.log(arry1);
console.log(arry2);

//test object
var obj1 = {name:"gino",sex:"male",age:27,favorite:"pingpang",family:{brother:"zhipeng",mother:"SunAiyun",father:"WangLicai"}};
var obj2 = deepClone(obj1);
obj1.family.brother = "close";
console.log(obj1);
console.log(obj2);



/*-------------------------1.第三种方案-------------------------------------------------------*/
var deepClone = function (o){

    var copy = Object.create( Object.getPrototypeOf(o) );
    var propNames = Object.getOwnPropertyNames(o);
    console.log(propNames)
    propNames.forEach(function(name){
        var desc = Object.getOwnPropertyDescriptor(o, name);
        Object.defineProperty(copy, name, desc);
    });

    return copy;
}

var deepClone=function (o) {
    var copy=Object.create(Object.getPrototypeOf(o));
    var propNames=Object.getOwnPropertyNames(o);
    propNames.forEach(function (name) {
        var desc=Object.getOwnPropertyDescriptor(o,name);
        Object.defineProperty(copy,name,desc)
    })
}


var obj1 = {family:{brother:"wangzhipeng",father:"wanglicai",mother:"sunaiyun"},name:"gino",sex:"male",age:"27"};
var obj2 = deepClone(obj1);
obj1.sex = "close";
console.log(obj1);
console.log(obj2);